<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\InvitationAuditActions;
use App\Admin\Actions\Grid\ViewAuditComments;
use App\Admin\Repositories\InvitationApply;
use Carbon\Carbon;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class InvitationApplyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new InvitationApply(), function (Grid $grid) {

            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->withBorder();
            $grid->addTableClass(['table-text-center']);

            $grid->model()->orderBy('id', 'desc');

            /**
             * 按钮控制.
             */
            // @permissions
            if (!Admin::user()->can('apply-create')) {
                $grid->disableCreateButton();
                // $grid->disableQuickCreateButton();
            }
            // @permissions
            if (!Admin::user()->can('apply-update')) {
                $grid->disableEditButton();
                // $grid->disableQuickEditButton();
            }
            // @permissions
            if (!Admin::user()->can('apply-delete')) {
                $grid->disableDeleteButton();
            }

            $grid->actions(function (Grid\Displayers\Actions $actions) {

                if ($this->status == -1 || $this->status == -2) {
                    if (Admin::user()->can('view-audit-comments')) {
                        // 查看审核意见
                        $actions->append(new ViewAuditComments());
                    }
                } else if ($this->status == 0) {
                    if (Admin::user()->can('audit-status')) {
                        // 审核
                        $actions->append(new InvitationAuditActions());
                    }
                }
            });

            $grid->column('id')->sortable();
            $grid->column('company_name');
            $grid->column('name');
            $grid->column('phone');
            $grid->column('email');
            $grid->column('status', '咨询状态')
                ->using(InvitationApply::getInvitationStatus())
                ->badge([
                    0 => '#C0C0C0',
                    1 => 'success',
                    -1 => 'danger',
                    -2 => 'warning'
                ]);
            // $grid->column('user_id');
            // $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->quickSearch(['company_name', 'name', 'phone', 'email'])->placeholder('请输入机构名称、姓名、手机号、邮箱');
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('status', '咨询状态', InvitationApply::getInvitationStatus());
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new InvitationApply(), function (Show $show) {
            $show->field('id');
            $show->field('company_name');
            $show->field('name');
            $show->field('phone');
            $show->field('email');
            $show->field('user_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new InvitationApply(), function (Form $form) {

            $form->disableViewButton();
            $form->disableViewCheck();

            // @permissions
            if (!Admin::user()->can('apply-delete')) {
                $form->disableDeleteButton();
            }

            // $form->display('id');
            $form->text('company_name');
            $form->text('name');
            $form->mobile('phone');
            $form->email('email');
            $form->radio('status', '咨询状态')
                ->options(InvitationApply::getInvitationStatus())
                ->when([-1, -2], function (Form $form) {
                    $form->textarea('audit_desc', '审核意见')
                        ->rules('required_if:status,-1,-2', ['required_if' => '请填写审核意见']);
                    $form->hidden('audit_time');
                });
            // $form->text('user_id');
            $form->saving(function (Form $form) {
                $form->audit_time = Carbon::now();
            });
        });
    }
}
